// pages/jishi/jishi.js
var util= require('../../utils/util.js');
Page({
 
data: {
  intervarID:"",
  thingid:"",
  thingname:"",
  first_time_diff:0,//最开始的时候的时间差，秒为单位
  now_time:0,//点击暂停时的时间差
  true_time:0,//真正被计时的时间
  daojishid:"",
  daojishiname:"",
  date:"",//当天日期

  hours: "",//小时
  minutes: "",//分
  seconds: "",//秒
  pause:0,
  background:[
    "0",
    "https://z3.ax1x.com/2021/05/01/gVwn8e.jpg",
    "https://z3.ax1x.com/2021/05/01/gVwmCD.jpg",
    "https://z3.ax1x.com/2021/05/01/gVwZ4O.jpg",
    "https://z3.ax1x.com/2021/05/01/gVwVUK.jpg",
    "https://z3.ax1x.com/2021/05/01/gVwEE6.jpg"
  ],
  randomIndex:1
},
//定时器
countDown: function () {
  var that = this;
  var now = that.data.now_time;//获取时间差
  var true_t = that.data.true_time;
  this.data.intervarID = setInterval(function () {//设置定时器
    //将时间差减一秒  
    now--;
    true_t++;//此次事件所执行的总时长
    that.setData({
      now_time:now,
      true_time:true_t
    })
    
  //计算天时分秒
    var h = parseInt(Math.floor((now % 86400) / 3600)) ;
    var m = parseInt(Math.floor((now% 3600) / 60)) ;
    var s = parseInt(now % 60);

    that.setData({
      hours:   (h<10) ? '0'+h : h,
      minutes: (m<10) ? '0'+m : m,
      seconds: (s<10) ? '0'+s : s,
    });
    //当时间差为0时,清除定时器
    if ( h <= 0 && m <= 0 && s <= 0) {//倒计时正常结束的时候
      clearInterval(that.data.intervarID);
      wx.cloud.callFunction({
        name:"createjishi",
        data:{
          thingid:that.data.thingid,
          thingname:that.data.thingname,
          trueduration:that.data.true_time,
          daojishid:that.data.daojishid,
          daojishiname:that.data.daojishiname,
          date:that.data.date
        }
      }).then(res=>{
        wx.showToast({
        title: '计时完成',
        icon:'success',
        duration:2000
      })
        wx.switchTab({
          url: '../../pages/shouye/shouye',
        })
      })

    }
  }, 1000)

},
pauseListener1(){//暂停
  var that = this;
  that.setData({pause:1})
  clearInterval(that.data.intervarID);
},
pauseListener2(){
  var that = this;
  that.setData({pause:0})
  that.countDown()
},
xunhuanListener(){ 
  var that = this;
  wx.showModal({
    title: '提示',
    content: '确定循环执行计时？',
    success (res) {
      if (res.confirm) {
        if(that.data.pause==1){//处于暂停的状态，即、定时器已经被清除了
          var now = that.data.first_time_diff;//获取时间差
          that.setData({
            now_time:now,
            pause:0
          });
          that.countDown()
        }
        else{
          clearInterval(that.data.intervarID);
          var now = that.data.first_time_diff;//获取时间差
          that.setData({
            now_time:now,
            pause:0
          });
          that.countDown()
        }   
      } else if (res.cancel) {
      }
    }
  })
},
stopListener(){
  var that = this;
  that.setData({pause:0})
  clearInterval(that.data.intervarID);
  wx.showModal({
    title: '提示',
    content: '确定不再坚持一下？',
    success (res) {
      if (res.confirm) {
        that.setData({
          now_time:0
        });
        //存入数据库
        if(that.data.true_time>60){
          wx.cloud.callFunction({
            name:"createjishi",
            data:{
              thingid:that.data.thingid,
              thingname:that.data.thingname,
              trueduration:that.data.true_time,
              daojishid:that.data.daojishid,
              daojishiname:that.data.daojishiname,
              date:that.data.date
            }
          });
        }
        wx.switchTab({
          url: '../../pages/shouye/shouye',
        })

      } else if (res.cancel) {
        that.setData({pause:0})
        that.countDown()
      }
    }
  })

},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  var ran_index = Math.ceil(Math.random()*5);//获取1-5之间的整数  
   console.log(options)
   var dateNow = util.formatTime(new Date());//获取当前执行这个计时的当天日期
   //console.log(dateNow.toString())
   var index = dateNow.indexOf(' ');
   dateNow = dateNow.substring(0,index )
    this.setData({
    date: dateNow,
    });
   var temp = parseInt(options.duration);//转成int类型
   var t = temp*60;
   this.setData({
    daojishid:options.daojishid,
    daojishiname:options.daojishiname,
    randomIndex:ran_index,
    thingid:options._id,
    first_time_diff : t,
    now_time:t,//刚刚开始加载页面的时候，now_time和first_time_diff是相等的
    thingname:options.thingname
   })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //定时器开始
    this. countDown();
    wx,wx.hideHomeButton({
      success: (res) => {},
      fail: (res) => {},
      complete: (res) => {},
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  }

})